package com.dalin.platform.autocode;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.dalin.common.PropertyConst;
import com.dalin.common.clazz.ArgList;
import com.dalin.common.clazz.Clazz;
import com.dalin.common.clazz.ImportList;
import com.dalin.common.clazz.Interfazz;
import com.dalin.common.clazz.Method;
import com.dalin.common.clazz.Note;
import com.dalin.common.clazz.Property;
import com.dalin.common.clazz.PropertyList;
import com.dalin.common.utils.FileUtils;
import com.dalin.common.utils.NumberUtils;
import com.dalin.common.utils.StringUtils;
import com.dalin.common.utils.TextUtils;
import com.dalin.framework.dao.ConnectFactory;
import com.dalin.framework.dao.DbConn;
import com.dalin.framework.vos.ColumnBaseInfoVo;
import com.dalin.framework.vos.JdbcVo;

/*****************************************************************************
 * @content: autocode工程静态属性及类方法                                                                                        *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2009-07-27                                                     *
 ****************************************************************************/
public final class SkyAutocodeConst {
	private static String NEXT_NJL_ID = "njlId27";
	public static String getNextNjlId() {
		return SkyAutocodeConst.NEXT_NJL_ID = NumberUtils
				.getNextStrNum(SkyAutocodeConst.NEXT_NJL_ID);
	}
	/*************************************************************************
	 * @content: 根据属性生成它的get/set方法
	 * @param  : propList 属性集合
	 * @return : List<Method>
	 * @author : 那建林
	 * @crtDate: 2009-09-17
	 * @modDate: 2010-05-11
	 * @version: 1.3 
	 */
	public static List<Method> getGetSetMethodList(PropertyList propList) {
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		StringBuffer buf = null;
		for (Property p : propList.getAll()) {
			String pType = p.getType();
			String pName = p.getName();
			String upperPName = pName.substring(0, 1).toUpperCase()
					+ pName.substring(1);
			// getMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
				.append(PropertyConst.RETURN_STR)
				.append(PropertyConst.SPACE).append(pName)
				.append(PropertyConst.SEMICOLON);
			method = new Method(PropertyConst.GET_STR + upperPName);
			method.setBody(buf.toString());
			method.setRtnType(pType);
			methodList.add(method);
			
			// setMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
				.append(PropertyConst.THIS_STR).append(PropertyConst.DOT)
				.append(pName).append(PropertyConst.SPACE)
				.append(PropertyConst.EQUAL).append(PropertyConst.SPACE)
				.append(pName).append(PropertyConst.SEMICOLON);
			ArgList argList = new ArgList();
			argList.add(pType, pName);
			method = new Method(PropertyConst.SET_STR + upperPName);
			method.setArgList(argList);
			method.setBody(buf.toString());
			methodList.add(method);
		}
		return methodList;
	}
	
	
	/*************************************************************************
	 * @content: 根据表名生成普通的映射javabean,属性及相应的get/set方法
	 * @param  : jdbc 数据库连接bean
	 * @param  : tableName 表名
	 * @param  : packageName 包名
	 * @return : Clazz
	 * @author : 那建林                                   
	 * @crtDate: 2009-10-03 
	 * @modDate: 2010-05-11
	 * @version: 1.3 
	 * @throws Exception 
	 */
	public static Clazz getJavaClazz(
			DbConn conn, String tableName, String packageName) throws Exception {
		Set<ColumnBaseInfoVo> list = conn.queryColumnInfosByTable(tableName);
		System.out.println(list.size());
		PropertyList propList = new PropertyList();
		
		for (ColumnBaseInfoVo vo : list) {
			String lowerName = StringUtils
					.underlineToLowerCamelCase(vo.getColumnName());
			String javaType = conn.getJavaTypeFromColumnType(
									vo.getColumnType(),
									vo.getColumnLength(), 
									vo.getDecimalDigits());
			StringBuffer noteBuf = new StringBuffer();
			noteBuf.append(vo.getColumnName()).append(PropertyConst.COMMA)
					.append(vo.getColumnLength()).append(PropertyConst.COMMA)
					.append(vo.getDecimalDigits());
			propList.add(javaType, lowerName, noteBuf.toString());
		}

		Clazz myClass = new Clazz();
		myClass.setPackageName(packageName);
		myClass.setClassName(
				StringUtils.underlineToUpperCamelCase(tableName) + "VO");
		myClass.setPropertyList(propList);
		myClass.setMethodList(getGetSetMethodList(propList));
		return myClass;
	}
	
	
	/*************************************************************************
	 * @content: 根据表名生成JPA注解映射bean
	 * @param  : jdbc 数据库连接bean
	 * @param  : tableName 表名
	 * @param  : packageName 包名
	 * @return : Clazz
	 * @author : 那建林                                   
	 * @crtDate: 2009-10-03 
	 * @modDate: 2010-05-11
	 * @version: 1.3 
	 * @throws Exception 
	 */
	public static Clazz getAnnotationClazz(
			DbConn conn, String tableName, String packageName) throws Exception {
		Set<ColumnBaseInfoVo> list = conn.queryColumnInfosByTable(tableName);
		PropertyList propList = new PropertyList();
		
		for (ColumnBaseInfoVo vo : list) {
			String lowerName = StringUtils
					.underlineToLowerCamelCase(vo.getColumnName());
			String javaType = conn.getJavaTypeFromColumnType(
									vo.getColumnType(),
									vo.getColumnLength(), 
									vo.getDecimalDigits());
			StringBuffer noteBuf = new StringBuffer();
			noteBuf.append(vo.getColumnName()).append(PropertyConst.COMMA)
					.append(vo.getColumnLength()).append(PropertyConst.COMMA)
					.append(vo.getDecimalDigits());
			propList.add(javaType, lowerName, noteBuf.toString());
		}

		Clazz myClass = new Clazz();
		myClass.setPackageName(packageName);
		myClass.setClassName(
				StringUtils.underlineToUpperCamelCase(tableName) + "Entity");
		myClass.setPropertyList(propList);
		myClass.setMethodList(getGetSetMethodList(propList));
		return myClass;
	}
	
	
	/*************************************************************************
	 * @content: 根据表名生成condition查询封装类
	 * @param  : jdbc 数据库连接bean
	 * @param  : tableName 表名
	 * @param  : packageName 包名
	 * @return : Clazz
	 * @author : 那建林                                   
	 * @crtDate: 2009-10-03 
	 * @modDate: 2010-05-27
	 * @version: 1.3 
	 */
	public static Clazz getConditionClazz(DbConn conn,
			String tableName, String packageName) throws Exception {
		Clazz cls = getAnnotationClazz(conn, tableName, packageName);
		// 引用哪些包
		//类名
		cls.setClassName(StringUtils.underlineToUpperCamelCase(tableName)
				+ "Cond");
		
		return cls;
	}
	
	
	/*************************************************************************
	 * @content: 根据表名生成创建service接口类
	 * @param  : jdbc 数据库连接bean
	 * @param  : tableName 表名
	 * @param  : packageName 包名
	 * @return : Interfazz
	 * @author : 那建林                                   
	 * @crtDate: 2009-10-03 
	 * @modDate: 2010-05-30
	 * @version: 1.3 
	 */
	public static Interfazz getServiceInterfaceClazz(String tableName,
			String packageName) throws Exception {
		String javaTableName = StringUtils.underlineToUpperCamelCase(tableName);
		Interfazz inter = new Interfazz();
		inter.setInterfaceName(javaTableName + "Service");
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		ArgList argList = new ArgList();
		/**
		 * 查询方法
		 */
		method = new Method("query" + javaTableName + "s");
		method.setRtnType("List");
		argList.add(javaTableName + "Condition", "cond");
		method.setArgList(argList);
		method.setMethodNote(Note.getMethodNote(method));
		method.setException("Exception");
		methodList.add(method);
		/**
		 * 查找单条记录方法
		 */
		method = new Method("find" + javaTableName + "ById");
		method.setRtnType(javaTableName + "DTO");
		argList = new ArgList();
		argList.add("Integer", "id");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 增加记录方法
		 */
		method = new Method("add" + javaTableName);
		argList = new ArgList();
		argList.add(javaTableName + "VO", "vo");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 修改记录方法
		 */
		method = new Method("modify" + javaTableName);
		argList = new ArgList();
		argList.add(javaTableName + "VO", "vo");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		/**
		 * 删除方法
		 */
		method = new Method("delete" + javaTableName + "s");
		argList = new ArgList();
		argList.add("Integer[]", "idAry");
		method.setArgList(argList);
		method.setException("Exception");
		method.setMethodNote(Note.getMethodNote(method));
		methodList.add(method);
		
		inter.setMethodList(methodList);
		return inter;
	}
	
	
	public static Clazz getServiceImpleClazz(DbConn conn, 
			String tableName, String packageName) throws Exception {
		String javaTableName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propList = getJavaClazz(conn, tableName, packageName)
				.getPropertyList();
		Interfazz inter = getServiceInterfaceClazz(tableName, packageName);
		Clazz cls = new Clazz();
		cls.setPackageName(packageName);
		cls.setClassName(inter.getInterfaceName() + "Impl");
		cls.setExtendsClassName("BaseServiceImpl");
		List<String> implementsList = new ArrayList<String>();
		implementsList.add(inter.getInterfaceName());
		cls.setImplementsList(implementsList);
		cls.setMethodList(inter.getMethodList());
		
		Method method = null;
		StringBuffer buf = null;
		/**
		 * 查询方法
		 */
		method = cls.getMethodList().get(0);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append("        List<"+javaTableName+"DTO> list = (List<"
				+javaTableName+"DTO>) getDbDao().selectList(\""+javaTableName
						+"Entity.selectList\", cond);");
		buf.append("\n        return list;");
		method.setBody(buf.toString());
		cls.getMethodList().set(0, method);
		/**
		 * 查找单条记录方法
		 */
		method = cls.getMethodList().get(1);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "DTO")
			.append(" enty = (").append(javaTableName + "DTO")
			.append(") getDao().findObjById(")
			.append(javaTableName + "DTO.class, id);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "DTO")
			.append(" vo = new ").append(javaTableName)
			.append("VO();").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("return DTO;");
		method.setBody(buf.toString());
		cls.getMethodList().set(1, method);
		/**
		 * 增加记录方法
		 */
		method = cls.getMethodList().get(2);
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
			.append(" enty = new ").append(javaTableName + "Entity();")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("enty = get")
			.append(javaTableName).append("EntityValue(enty, vo);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("getDao().createObj(enty);");
		method.setBody(buf.toString());
		cls.getMethodList().set(2, method);
		/**
		 * 修改记录方法
		 */
		method = cls.getMethodList().get(3);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append("Long id = vo.get")
			.append(javaTableName + "Id();").append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append(javaTableName + "Entity")
			.append(" enty = (").append(javaTableName + "Entity")
			.append(") getDao().findObjById(")
			.append(javaTableName + "Entity.class, id);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("enty = get")
			.append(javaTableName).append("EntityValue(enty, vo);")
			.append(PropertyConst.LINE_SEPARATOR);
		buf.append(PropertyConst.TWO_TAB).append("getDao().updateObjNbv(enty);");
		method.setBody(buf.toString());
		cls.getMethodList().set(3, method);
		/**
		 * 批量删除方法
		 */
		method = cls.getMethodList().get(4);
		method.setException("Exception");
		buf = new StringBuffer();
		buf.append(PropertyConst.TWO_TAB).append("getDao().deleteObj(")
			.append(javaTableName).append("Entity.class, idArray);");
		method.setBody(buf.toString());
		cls.getMethodList().set(4, method);
		/**
		 * 增加属性值copy方法
		 */
		method = new Method("get" + javaTableName + "EntityValue");
		method.setRtnType(javaTableName + "Entity");
		method.setModifier(PropertyConst.PRIVATE_STR);
		ArgList argList = new ArgList();
		argList.add(javaTableName + "Entity", "enty");
		argList.add(javaTableName + "VO", "vo");
		method.setArgList(argList);
		method.setBody(setPropertyValue(propList, "enty", "vo")+"\n        return enty");
		cls.getMethodList().add(method);
		return cls;
	}

	
	public static Clazz getControllerClazz(String serviceName, String tableName,
			String packageName) throws Exception {
		String serviceRefName = serviceName.substring(0, 1).toLowerCase() 
				+ serviceName.substring(1);
		String upperJavaName = StringUtils.underlineToUpperCamelCase(tableName);
		PropertyList propertyList = new PropertyList();
		propertyList.add(new Property(serviceName, serviceRefName, null));
		List<Method> methodList = new ArrayList<Method>();
		Method method = new Method("describle"+upperJavaName+"s");
		ArgList argList = new ArgList();
		argList.add(StringUtils.underlineToUpperCamelCase(tableName) + "Cond", "cond");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/"+"describle"+upperJavaName+"s"+"\")\n    @ResponseBody");
		method.setRtnType("Object");
		StringBuffer buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append("return getSuccessValue(najlService.describe"+upperJavaName+"s(cond));\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(e);\n");
		buf.append(PropertyConst.THREE_TAB).append("return getErrorValue(\"error\");\n");
		buf.append(PropertyConst.TWO_TAB).append("}");
		method.setBody(buf.toString());
		methodList.add(method);
		
		method = new Method("add"+upperJavaName);
		argList = new ArgList();
		argList.add(StringUtils.underlineToUpperCamelCase(tableName) + "VO", "vo");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/"+"add"+upperJavaName+"\")\n    @ResponseBody");
		method.setRtnType("Object");
		buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append("return najlService.add"+upperJavaName+"(vo);\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(e);\n");
		buf.append(PropertyConst.THREE_TAB).append("return getErrorValue(\"error\");\n");
		buf.append(PropertyConst.TWO_TAB).append("}\n");
		buf.append(PropertyConst.TWO_TAB).append("return getSuccessValue(\"OK\");");
		method.setBody(buf.toString());
		methodList.add(method);
		
		method = new Method("modify"+upperJavaName);
		argList = new ArgList();
		argList.add(StringUtils.underlineToUpperCamelCase(tableName) + "VO", "vo");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/"+"modify"+upperJavaName+"\")\n    @ResponseBody");
		method.setRtnType("Object");
		buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append("return najlService.modify"+upperJavaName+"(vo);\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(e);\n");
		buf.append(PropertyConst.THREE_TAB).append("return getErrorValue(\"error\");\n");
		buf.append(PropertyConst.TWO_TAB).append("}\n");
		buf.append(PropertyConst.TWO_TAB).append("return getSuccessValue(\"OK\");");
		method.setBody(buf.toString());
		methodList.add(method);
		
		method = new Method("delete"+upperJavaName);
		argList = new ArgList();
		argList.add("Integer[]", "idAry");
		method.setArgList(argList);
		method.setMethodNote("@RequestMapping(\"/"+"delete"+upperJavaName+"\")\n    @ResponseBody");
		method.setRtnType("Object");
		buf = new StringBuffer();
		buf.append("        try{\n");
		buf.append(PropertyConst.THREE_TAB).append("return najlService.delete"+upperJavaName+"s(idAry);\n");
		buf.append(PropertyConst.TWO_TAB).append("} catch (Exception e) {\n");
		buf.append(PropertyConst.THREE_TAB).append("e.printStackTrace();\n");
		buf.append(PropertyConst.THREE_TAB).append("log.error(e);\n");
		buf.append(PropertyConst.THREE_TAB).append("return getErrorValue(\"error\");\n");
		buf.append(PropertyConst.TWO_TAB).append("}\n");
		buf.append(PropertyConst.TWO_TAB).append("return getSuccessValue(\"OK\");");
		method.setBody(buf.toString());
		methodList.add(method);
		
		List<Method> getSetMethodList = buildGetSetMethodByProperty(
				propertyList);
		for (Method m : getSetMethodList) {
			methodList.add(m);
		}
		
		Clazz cls = new Clazz();
		cls.setNote(cls.getNote()+"\n@Controller\n@RequestMapping(\"/发布路径\")\n");
		cls.setPackageName(packageName);
		ImportList importList = new ImportList();
		importList.add("java.util.ArrayList;");
		importList.add("java.util.List;");
		importList.add("org.springframework.beans.factory.annotation.Autowired;");
		importList.add("org.springframework.stereotype.Controller;");
		importList.add("org.springframework.web.bind.annotation.RequestMapping;");
		importList.add("org.springframework.web.bind.annotation.RequestParam;");
		importList.add("org.springframework.web.bind.annotation.ResponseBody;");
		importList.add("com.skyform.api.base.BaseController;");
		cls.setImportList(importList);
		cls.setClassName(upperJavaName + "Controller");
		cls.setExtendsClassName("BaseController");
		cls.setPropertyList(propertyList);
		cls.setMethodList(methodList);
		return cls;
	}
	
	
	
	// 增删查改
	public static void crudAndExts(DbConn conn,
			String tableName, String packageName) throws Exception {
		String savedDir = "d:/tmp/crud/"+ tableName + "/";
		File f = new File(savedDir);
		FileUtils.deleteFiles(f);
		f.mkdirs();

		// DTO值对象
		Clazz myClazz = getJavaClazz(conn, tableName, packageName);
		myClazz.setClassName(myClazz.getClassName().replace("VO", "DTO"));
		TextUtils.overwriteToText(myClazz.toString(),
				new File(savedDir + myClazz.getClassName().replace("VO", "DTO") + ".java"));
		
		// VO值对象
		myClazz = getJavaClazz(conn, tableName, packageName);
		TextUtils.overwriteToText(myClazz.toString(),
				new File(savedDir + myClazz.getClassName() + ".java"));
		// Annotation值对象
		myClazz = getAnnotationClazz(conn, tableName, packageName+".models");
		TextUtils.overwriteToText(myClazz.toString(),
				new File(savedDir + myClazz.getClassName() + ".java"));
		
		// Condition查询条件对象
		myClazz = getConditionClazz(conn, tableName, packageName);
		TextUtils.overwriteToText(myClazz.toString(),
				new File(savedDir + myClazz.getClassName() + ".java"));
		
		// service接口类
		Interfazz fazz = getServiceInterfaceClazz(tableName, packageName);
		TextUtils.overwriteToText(fazz.toString(),
				new File(savedDir + fazz.getInterfaceName() + ".java"));
		
		// service接口实现类
		myClazz = getServiceImpleClazz(conn, tableName, packageName);
		TextUtils.overwriteToText(myClazz.toString(),
				new File(savedDir + myClazz.getClassName() + ".java"));
		
		// 增加修改action类
		myClazz = getControllerClazz(
				"CustomerManageService", tableName, packageName);
		TextUtils.overwriteToText(myClazz.toString(), 
				new File(savedDir + myClazz.getClassName() + ".java"));
	}
	

	//把一个类的属性赋给另一个类的属性
	public static String setPropertyValue(PropertyList propList,
			String setRef, String getRef) {
		StringBuffer buf = new StringBuffer();
		for (Property p : propList.getAll()) {
			String pName = p.getName();
			pName = pName.substring(0, 1).toUpperCase() + pName.substring(1);
			buf.append(PropertyConst.TWO_TAB).append(setRef)
				.append(".set").append(pName).append("(").append(getRef)
				.append(".get").append(pName).append("()); // ")
				.append(PropertyConst.LINE_SEPARATOR);
		}
		return buf.toString();
	}
	
	/*************************************************************************
	 * @content: 根据属性生成它的get/set方法
	 * @param  : propList 属性集合
	 * @return : List<Method>
	 * @author : 那建林
	 * @crtDate: 2009-09-17
	 */
	public static List<Method> buildGetSetMethodByProperty(
			PropertyList propList) {
		List<Method> methodList = new ArrayList<Method>();
		Method method = null;
		StringBuffer buf = null;
		for (Property p : propList.getAll()) {
			String pType = p.getType();
			String pName = p.getName();
			String upperPName = pName.substring(0, 1).toUpperCase()
					+ pName.substring(1);
			// getMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
				.append(PropertyConst.RETURN_STR)
				.append(PropertyConst.SPACE).append(pName)
				.append(PropertyConst.SEMICOLON).append(PropertyConst.LINE_SEPARATOR);
			method = new Method(PropertyConst.GET_STR + upperPName);
			method.setBody(buf.toString());
			method.setRtnType(pType);
			methodList.add(method);
			
			// setMethod
			buf = new StringBuffer();
			buf.append(PropertyConst.TAB).append(PropertyConst.TAB)
				.append(PropertyConst.THIS_STR).append(PropertyConst.DOT)
				.append(pName).append(PropertyConst.SPACE)
				.append(PropertyConst.EQUAL).append(PropertyConst.SPACE)
				.append(pName).append(PropertyConst.SEMICOLON)
				.append(PropertyConst.LINE_SEPARATOR);
			ArgList argList = new ArgList();
			argList.add(pType, pName);
			method = new Method(PropertyConst.SET_STR + upperPName);
			method.setArgList(argList);
			method.setBody(buf.toString());
			methodList.add(method);
		}
		return methodList;
	}
	
	

	
	/**
	 * 
	 */
	public static void buildSkyFormMybatisCode() {
		JdbcVo jdbc = new JdbcVo();
		jdbc.setDriverClassName(PropertyConst.DRIVER_MYSQL);
		jdbc.setHostIp("10.10.182.170");          // 主机IP
		jdbc.setPort("3306");            // 端口号
		jdbc.setDbName("zstack");      // 数据库名
		jdbc.setUserName("root");        // 用户名
		jdbc.setUserPswd("password");        // 密码
		DbConn conn;
		try {
			conn = ConnectFactory.getConnectionInstance(jdbc);
			Set<String> tts = conn.queryTables();
			for (String s : tts) {
				if ("sf_vminstanceeo_ex".equalsIgnoreCase(s)) {
					System.out.println(s);
					buildSkyFormMybatisCode1(conn, s);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}	
	public static void buildSkyFormMybatisCode1(DbConn conn, String tableName) {
		String key = "ID";
		String keyc = "id";
		String ucc = StringUtils.underlineToUpperCamelCase(tableName);
		try {
			crudAndExts(conn, tableName, "com.skyform.core.entity");
			
			StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
			sb.append("<!DOCTYPE mapper  \n");
			sb.append("PUBLIC \"-//ibatis.apache.org//DTD Mapper 3.0//EN\"  \n");
			sb.append("\"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd\">\n");
			sb.append("<mapper namespace=\""+ucc+"Entity\">\n");
			sb.append("<select id=\"selectList\" parameterType=\"com.skyform.core.condition."+ucc+"Cond\" resultType=\"com.skyform.core.entity."+ucc+"Entity\">\n");
			sb.append("    select\n");
			Set<ColumnBaseInfoVo> cs = conn.queryColumnInfosByTable(tableName); 
			int i = 0;
			for (ColumnBaseInfoVo ci : cs) {
				sb.append("        t."+ci.getColumnName()+" as "+StringUtils.underlineToLowerCamelCase(ci.getColumnName()));
				if (++i == cs.size()) {
					sb.append("\n");
				} else {
					sb.append(", \n");
				}
			}
			sb.append("    from "+tableName+" t where 1=1\n");
			sb.append("</select>\n");
			sb.append("<delete id=\"delete\" parameterType=\"java.lang.Integer\">\n");
			sb.append("    delete from "+tableName+" where "+key+"=#{"+keyc+"}\n");
			sb.append("</delete>\n<insert id=\"insert\" parameterType=\"com.skyform.core.entity."+ucc+"Entity\">\n");
			sb.append("    insert into "+tableName+"(\n");
			i = 0;
			for (ColumnBaseInfoVo ci : cs) {
				sb.append("        "+ci.getColumnName());
				if (++i == cs.size()) {
					sb.append("\n");
				} else {
					sb.append(", \n");
				}
			}
			sb.append("    ) values (\n");
			i = 0;
			for (ColumnBaseInfoVo ci : cs) {
				sb.append("        #{"+StringUtils.underlineToLowerCamelCase(ci.getColumnName())+"}");
				if (++i == cs.size()) {
					sb.append("\n");
				} else {
					sb.append(", \n");
				}
			}
			sb.append("    )\n");
			sb.append("</insert>\n<update id=\"update\" parameterType=\"com.skyform.core.entity."+ucc+"Entity\">\n");
			sb.append("    update "+tableName+" \n");
			sb.append("		<set>\n");
			i = 0;
			for (ColumnBaseInfoVo ci : cs) {
				sb.append("			<if test=\""+StringUtils.underlineToLowerCamelCase(ci.getColumnName())+" != null\">"+ci.getColumnName()+" = #{"+StringUtils.underlineToLowerCamelCase(ci.getColumnName())+"}");
				if (++i == cs.size()) {
					sb.append("</if>\n\n");
				} else {
					sb.append(",</if>\n");
				}
			}
			sb.append("		</set>\n");
			sb.append("		where "+key+" = #{"+keyc+"}\n");
			sb.append("</update>\n</mapper>\n");
			TextUtils.overwriteToText(sb.toString(), new File("d:/tmp/crud/"+ucc+"Entity.xml"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void compareTwoTables() {
		JdbcVo jdbc = new JdbcVo();
		jdbc.setDriverClassName(PropertyConst.DRIVER_MYSQL);
		jdbc.setHostIp("10.10.214.100");          // 主机IP
		jdbc.setPort("3306");            // 端口号
		jdbc.setDbName("skyform_development_njl");      // 数据库名
		jdbc.setUserName("skycloud");        // 用户名
		jdbc.setUserPswd("skycloud");        // 密码
	
		JdbcVo jdbc2 = new JdbcVo();
		jdbc2.setDriverClassName(PropertyConst.DRIVER_MYSQL);
		jdbc2.setHostIp("192.168.31.155");          // 主机IP
		jdbc2.setPort("3306");            // 端口号
		jdbc2.setDbName("skyform_development");      // 数据库名
		jdbc2.setUserName("skycloud");        // 用户名
		jdbc2.setUserPswd("skycloud");        // 密码
		
		try {
			DbConn conn = ConnectFactory.getConnectionInstance(jdbc);
			Set<String> set = conn.queryTables();
			DbConn conn2 = ConnectFactory.getConnectionInstance(jdbc2);
			Set<String> set2 = conn2.queryTables();
			for (String name : set) {
				if (!set2.contains(name)) {
					System.out.println("无此表:" + name);
					continue;
				}
				Set<ColumnBaseInfoVo> cs = conn.queryColumnInfosByTable(name);
				Set<ColumnBaseInfoVo> cs2 = conn2.queryColumnInfosByTable(name);
				for (ColumnBaseInfoVo ci : cs) {
					if (!cs2.contains(ci)) {
						System.out.println("       表[" + name + "]中无字段:" + ci.getColumnName());
					} else {
						for (ColumnBaseInfoVo ci2 : cs2) {
							if (ci2.getColumnName().equals(ci.getColumnName())) {
								if (!ci2.getColumnType().equals(ci.getColumnType())) {
									System.out.println("       表[" + name + "]中字段["+ci.getColumnName()+"]类型不同, 应为:" + ci.getColumnType());
								} else if (ci2.getColumnLength() != ci.getColumnLength()) {
									System.out.println("       表[" + name + "]中字段["+ci.getColumnName()+"]长度不同, 应为:" + ci.getColumnLength());
								}
							}
						}
					}
				}
			}
			System.out.println("over");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		buildSkyFormMybatisCode();
		//compareTwoTables();
	}
}